<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <script>
      // 1.移除数组 [1, 2, 3, 4, 2] 中的2。不要直接修改数组，结果返回新的数组
    arr1 =  [1, 2, 3, 4, 2];
    sum1 = [];
    sum1 = arr1.filter(function(value, index){
      return value !== 2;
    });
    console.log('sum1',sum1);

      // 2.定义一个函数,能在数组 [1, 2, 3, 4, 2] 的 "2"后面添加元素 "JavaScript"。不要直接修改数组 arr，结果返回新的数组
      let  arr2 = [1, 2, 3, 4, 2];
    let  sum2 = [];
    sum2 = arr2.reduce(function(total, current){
      if(current !=2){
        total.push(current);
      }else{
            total.push(current+','+'JavaScript');
      }
      return  total;
    },[]);
    console.log('sum2',sum2);
    
      // 3.统计数组 [1, 2, 3, 4, 2] 中2出现的次数
      let arr3 = [1,2,3,4,2];
      let sum3 = 0;
      sum3 = arr3.reduce(function(total, current, index, array){
      if( current === 2){
      return total=total+1;
      }
      return total;   
      },0);
      console.log('sum3',sum3);
      // 4.找出数组 [1, 2, 3, "JavaScript", 4, "JavaScript", 2, 4, "JavaScript"] 中重复出现过的元素,并用数组将重复元素装起来输出
      let arr4 = [1, 2, 3, "JavaScript", 4, "JavaScript", 2, 4, "JavaScript"];
      let sum4  = [];
     for (let i = 0; i < arr4.length; i++) {
     if (!sum4.includes(arr4[i])) {
    sum4.push(arr4[i]);
    }
    }
    console.log(sum4); 
      // 5.在数组里面输出年龄小于17的对象
      let arr5 = [
        {
          name: '111',
          sex: 'boy',
          age: 18,
        },
        {
          name: '222',
          sex: 'girl',
          age: 17,
        },
        {
          name: '333',
          sex: 'boy',
          age: 16,
        },
        {
          name: '444',
          sex: 'girl',
          age: 15,
        },
        {
          name: '555',
          sex: 'boy',
          age: 20,
        },
      ];
      for (let i = 0; i < arr5.length; i++) {
        if (arr5[i].age < 17) {
          console.log(arr5[i]);
        }
      }

      // 6. 数组扁平化, 把数组[1, [2, [3, 4, 5]]]转成[1, 2, 3, 4，5] (多维数组 --> 一维数组)

      // 7. 使用reduce实现[1,2,3,4,9]数组中所有成员的和
      let arr = [1,2,3,4,9];
      let sum = [];
      sum = arr.reduce(function(total, current, index, array){
        return total += current;
    },0);
    console.log(sum);
      // 8. 数组去重的方法 (4种)
    let arr8 = [1, 2, 3, 2, 1, 4, 5, 4];
    let sum8 = Array.from(new Set(arr8));
    console.log(sum8); 

    let array = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4];
    let uniqueArray = [];
    for (let i = 0; i < array.length; i++) {
    if (!uniqueArray.includes(array[i])) {
    uniqueArray.push(array[i]);
    }
  }
    console.log(uniqueArray); 
    </script>
  </body>
</html>
